# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：ffmpeg_model.py
#日期：2019-06-12
#备注：通过ffmpeg库处理视频信息   pip install ffmpeg-python ，在这之前还需要安装windows版本FFmpeg
      https://ffmpeg.zeranoe.com/builds/
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import ffmpeg
import subprocess
import os
from pycacho.cachobase.deal_json import dict_to_json
import time


def get_info(file):
    info = ffmpeg.probe(file)
    dict_to_json(info)
    return info

# out_name-->'image_%03d'-->image_001; time-->视频时长；video-->视频；image_path-->图片截取后目录,out_name-->_%03d
def get_image(video, image_path,time,out_name):
    img_count = 1
    crop_time = 0.0
    while crop_time <= time:          # 截取前15s的视频
        os.system('ffmpeg -i %s -y -ss %s -t 1 -r 1 -q:v 2 -f image2 %s.jpg'%
                  (video, str(crop_time), image_path +out_name%img_count))  #
        img_count += 1
        #print('Geting Image ' + str(img_count) + '.jpg' + ' from time ' + str(crop_time))
        crop_time += 0.04        # 25帧0.04 30帧100/30=
    print('视频转化完成！！！')
    return


def img_to_video():
    os.system('ffmpeg -i F:\image\\temp\\'+'image_%03d.jpg -y -r 25 F:\image\lkx\\01\\04.mp4')


# 剪切视频、转换视频
def cut_video(video_path, output_path):
    # os.system()：Windows在cmd里面执行语句
    #os.system('ffmpeg -i %s -t 18 -c:a copy -c:v copy -y %s' % (video_path,output_path))    # 可能缺失画面时用libx264代替（重新编码） -c:v libx264
    #print('剪切的同时，音视频参数不变，格式不变，')

    os.system('ffmpeg -i %s -t 15.5 -c:a copy -crf 16 -y %s' % (video_path, output_path))
    #print('剪切的同时，音视频参数不变，只转换格式')

    #os.system('ffmpeg -i %s -y -max_muxing_queue_size 1024 -b:v 1200k -bufsize 1200k -maxrate 1500k -strict -2 -s 1280x720 -threads 4 %s' % (video_path, output_path))
    #print('剪切的同时，只转换音视频参数，格式不变')      # 增大容器封装队列大小：-max_muxing_queue_size 1024

    #os.system("ffmpeg - i 'https://zhihu-web-analytics.zhihu.com/api/v1/logs/batch' -y test.mp4")
    #print('对外部视频下载并转为本地的')

# 合并视频前进行转换
def pre_concat_video(output_path):
    # 先将mp4转换为ts格式，然后再合并并且输出为mp4格式
    os.system('ffmpeg -i %s -c:a copy -c:v copy -vbsf h264_mp4toannexb 1.ts' % ('F:\image\\temp\\a-copy.mp4'))

# 合并视频
def concat_video(output_path):
    #os.system('ffmpeg -i "concat:1.mpg|2.mp4|3.mpg" -y -c copy %soutput.mp4' % ( output_path))    # 只能合并mpeg格式.mpg
    # 先将mp4转换为ts格式，然后再合并并且输出为mp4格式
    os.system('ffmpeg -i "concat:1.ts|2.ts" -vcodec copy -acodec copy -absf aac_adtstoasc %s'% (output_path))

# 视频尺寸修改
def scale_video(video_path,output_path):
    os.system('ffmpeg -i %s -c:a copy -crf 18 -s 960x1280 -y %s' % (video_path,output_path))   # 固定尺寸
    # 按照比例缩放 固定高576   scale=400:-1 固定宽400 ; scale=320:240 ; scale=iw*0.9:ih*0.9
    #os.system('ffmpeg -i %s -y -strict -2 -vf scale=360:-1 %s' % (video_path,output_path))

# 视频翻转:2-逆时针90，1-顺时针90
def rotate_video(video_path,output_path):
    os.system('ffmpeg -i %s -y -c:a copy -crf 20 -s 720x1280 -vf transpose=1 %s' % (video_path,output_path))   #

# 视频裁剪
def crop_video(video_path,output_path):
    os.system('ffmpeg -i %s -y -vf crop=958:1085:1:0.6 -ss 1 -t 43 -crf 16 %s' % (video_path,output_path))     # 裁剪画面：width:height: x:y，其中width 和height 表示裁剪后的尺寸，x:y 表示裁剪区域的左上角锚点的坐标。
# crop=1051:588.5:114:0.6   crop=1278.5:540:0.6:27   crop=718.5:450:0.6:430

# 视频加速减速 >1是减速
def speed_video(video_path,output_path):
    os.system('ffmpeg -i %s -y -vf "setpts=0.9*PTS" %s' % (video_path, output_path))

# 提取纯视频
def get_video(video_path, output_path):
    os.system('ffmpeg -i %s -y -c:v copy -an %s' % (video_path, output_path))      # 提取视频

# 拼接视频vstack-上下，hstack-水平
def compose_video(output_path):
    os.system('ffmpeg -i %s -i %s -filter_complex [0][1]hstack -y %s' % ('F:\image\\1.mp4','F:\image\\2.mp4',output_path))      #

# 使用 ffmpeg 获取 I 帧
def get_I_image(video_path):
    os.system('ffmpeg -i %s -ss 00:00:00 -t 10 -r 1 -q:v 2 -f image2 F:\image\\3d.jpeg' % (video_path))

def get_gif(video_path, output_path):
    os.system('ffmpeg -i %s -vframes 10 -y -f gif %s' % (video_path,output_path))   # 视频前10秒转gif文件

# 去除或者删除logo(ffplay()先去定位具体位置)
def logo_video(video_path,output_path):
    os.system('ffmpeg -i %s -vf delogo=x=380:y=635:w=500:h=84 -y %s' % (video_path, output_path))
    #os.system('ffmpeg -i "F:\image\\lkx\\01\\03.mp4" -vf delogo=x=500:y=610:w=270:h=107 -y "F:\image\\lkx\\03a.mp4"')
    print('去除视频logo')
    #os.system('ffmpeg -i %s -i logo.png -filter_complex overlay -y %s' % (video_path,output_path))
    print('对视频添加logo')
    #os.system('ffmpeg -i %s -vf subtitles=e.srt -y %s' % (video_path,output_path))
    print('对视频添加文字')

def ff_play():
    #os.system('ffplay.exe %s'% (video_path))        # 播放视频
    # xy 指定坐标，wh宽和高,(利用周围像素进行插值)band缺省值4,参数 band 的值不宜设置过大,参数 show 应使用缺省值0, 1的时候显示一个绿色框框
    os.system('ffplay -i %s -vf delogo=x=380:y=635:w=500:h=84:show=1 '% (video_path))
    #os.system('ffplay -i %s ' % (video_path))       #  -vf subtitles=e.srt 字幕
    # x=0.6:y=27:w=1278.5:h=540
    # x=114:y=0.6:w=1051:h=588.5

# 视频+音频合并
def audio_video(video_path,audio_path):
    os.system('ffmpeg -i F:\image\output\q1.mp4 -i F:\image\\music\\1.aac -y F:\image\9d.mp4' )

def main(t):
    sTime = time.time()
    if t == 0:
        get_info(video_path)
    elif t == 1:
        cut_video(video_path, output_path)
    elif t == 2:
        scale_video(video_path,output_path)
    elif t == 3:
        rotate_video(video_path, output_path)
    elif t == 4:
        ff_play()
    elif t == 5:
        crop_video(video_path, output_path)
    elif t == 6:
        pre_concat_video(output_path)
    elif t == 7:
        concat_video(output_path)
    elif t == 8:
        compose_video(output_path)
    elif t == 9:
        logo_video(video_path, output_path)
    elif t == 10:
        img_to_video()
    elif t == 11:
        get_image(video_path,output_path,16,'%03d')
    elif t == 12:
        audio_video(video_path,audio_path)
    elif t == 13:
        speed_video(video_path,output_path)
    # logo_video()
    # get_I_image(video_path)
    eTime = time.time()
    s = eTime - sTime
    print('花费的时间为：%.2f秒' % (s))


if __name__ == '__main__':
    video_path = 'F:\\alibaba\产品图片\\S23PRO.MP4'
    output_path = 'F:\\alibaba\产品图片\\temp\\S23PRO-1.MP4'  # F:\\alibaba\产品图片\\temp\\
    audio_path = 'F:\image\百岁之好\百岁之好-01.aac'
    main(13)
    # 0-查询，1-剪切、修改码率，2-缩放，3-翻转视频，4-播放，5-截取视频，6-预合并前转换，7-合并视频，11-获取图片,13-加减速
